package com.oa.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.CertificationBodySaveDTO;
import com.oa.admin.entity.CertificationBody;
import com.oa.admin.service.ICertificationBodyService;
import com.oa.core.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/certificationBody")
@Api(description = "认证机构模块")
public class CertificationBodyController extends BaseController {

    private final ICertificationBodyService certificationBodyService;

    @Autowired
    public CertificationBodyController(ICertificationBodyService certificationBodyService) {
        this.certificationBodyService = certificationBodyService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询（不传page则查询所有）")
    public Resp<List<CertificationBody>> findList(
            @ApiParam(value = "类别") @RequestParam(required = false) String type,
            @ApiParam(value = "机构名称") @RequestParam(required = false) String name,
            @ApiParam(value = "页码") @RequestParam(required = false) Integer pageNum,
            @ApiParam(value = "查询条数") @RequestParam(required = false) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        Wrapper wrapper = new EntityWrapper();
        if(StringUtils.isNotBlank(type)){
            wrapper.like("type", type);
        }else if(StringUtils.isNotBlank(name)){
            wrapper.like("name", name);
        }
        return new Resp().success(new PageResult(certificationBodyService.selectPage(page, wrapper).getRecords(), page));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<CertificationBody> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(certificationBodyService.findCertificationBodyDetails(id));
    }


    /**
     * 新增
     *
     * @param certificationBody CertificationBody
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "认证机构对象") @RequestBody(required = false) CertificationBodySaveDTO certificationBodySaveDTO) {
        return certificationBodyService.add(certificationBodySaveDTO);
    }

    /**
     * 根据Id修改
     *
     * @param certificationBody CertificationBody
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "认证机构对象") @RequestBody(required = false) CertificationBodySaveDTO certificationBodySaveDTO) {
        return certificationBodyService.edit(certificationBodySaveDTO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        CertificationBody certificationBody = certificationBodyService.selectById(id);
        if (certificationBody != null && certificationBody.getIsDeleted() == 2) {
            certificationBody.setIsDeleted(1);
            if (certificationBodyService.updateById(certificationBody)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
